/*
 * Copyright 2011 The Rusted Roof, LLC.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package org.proteusframework.api.base;

import org.proteusframework.api.IPlatformDelegate;
import org.proteusframework.api.model.IObject;
import org.proteusframework.api.model.IQualifiedName;
import org.proteusframework.util.Assert;


/**
 * Base class of all {@link IObject} objects.
 *
 * @author Tacoma Four
 */
public abstract class AbstractObject implements IObject
{
    /**
     * Immutable qualified name for this object.
     */
    final protected IQualifiedName qn;

    /**
     * Injected platform delegate used to access platform services, including locating and instantiating other
     * Proteus objects.
     */
    protected IPlatformDelegate delegate = null;

    /**
     * Defines a Proteus object with the associated qualified name.
     *
     * @param qualifiedName Non-null, immutable qualified name.
     */
    protected AbstractObject(IQualifiedName qualifiedName)
    {
        Assert.parameterNotNull(qualifiedName, "Parameter 'qualifiedName' must not be null");
        qn = qualifiedName;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String getNamespace()
    {
        return qn.getNamespace();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String getId()
    {
        return qn.getId();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String getRefId()
    {
        return qn.getRefId();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Object getAdapter(Class adapter)
    {
        Assert.parameterNotNull(adapter, "Parameter 'adapter' must not be null");
        return delegate.getAdapterManager().getAdapter(this, adapter);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void setPlatformDelegate(IPlatformDelegate delegate)
    {
        Assert.unitialized(this.delegate, "Platform delegate is implicitly final and cannot be modified.");

        Assert.parameterNotNull(delegate, "Parameter 'delegate' must not be null");

        this.delegate = delegate;
        onDelegateInjected();
    }

    /**
     * Empty delegate injection event handler for descendent classes.
     */
    protected void onDelegateInjected()
    {

    }

    /**
     * Equality is determined by comparing the {@link IQualifiedName} instances of this object and the comparison
     * object.
     */
    @Override
    public boolean equals(Object o)
    {
        if (this == o) return true;
        if (!(o instanceof AbstractObject)) return false;

        AbstractObject that = (AbstractObject) o;

        if (!qn.equals(that.qn)) return false;

        return true;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public int hashCode()
    {
        return qn.hashCode();
    }
}
